package com.qianfeng.zhangwei.xutils;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.qianfeng.zhangwei.xutils.bean.User;
import com.qianfeng.zhangwei.xutils.db.DaoFactory;

import java.io.File;
import java.util.List;

/**
 * HttpUtils
 * 1>数据加载
 * 2>数据提交
 * 3>文件上传
 *
 *
 *
 *
 * 4>文件下载
 */

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    private String url = "http://nirvana.fruitday.com/api";
    HttpUtils httpUtils = new HttpUtils();
    private String downUrl = "http://music.baidutt.com/up/kwcawscy/cmypus.mp3";
    @ViewInject(R.id.progress)
    private TextView progress;
    @ViewInject(R.id.add)
    private Button addBtn;
    @ViewInject(R.id.find)
    private Button findBtn;
    @ViewInject(R.id.delete)
    private Button deleteBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);

    }

    private void add() {
        User user = new User();
        user.setUserId("1");
        user.setName("1504");
        user.setPsw("123");
        DaoFactory.getUserDao().save(user);
    }

    private void downLoader() {
        /**
         * 第一个参数  下载url
         * 第二个参数 文件下载的路径
         * 第三个参数  下载回调监听
         */
        // sdk/qianfeng/video/
        String downFile = this.getCacheDir() + File.separator + "qianfeng/video";

        HttpHandler<File> httpHandler = httpUtils.download(downUrl, downFile, new RequestCallBack<File>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
                progress.setText("当前进度:" + current + "/" + total);
            }

            @Override
            public void onSuccess(ResponseInfo<File> responseInfo) {
                Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
                progress.setText("下载完成");
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

//        httpHandler.pause();

    }

    private void find() {
        List<User> list = DaoFactory.getUserDao().findAll();
        Log.e("user", list.toString());
    }

    @OnClick({R.id.add, R.id.find})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add:
                add();
                break;
            case R.id.find:
                find();
                break;
        }

    }


}
